package com.ruifu.act.contorller;

import com.fhzncloud.cloud.common.core.util.PageR;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.act.service.ImageService;
import com.ruifu.act.service.ProcessActService;
import com.ruifu.act.vo.AuditRecordVO;
import com.ruifu.common.vo.actanddoc.ActivitiVO;
import com.ruifu.common.vo.actanddoc.Flag;
import com.ruifu.common.vo.actanddoc.ProccessTaskVO;
import com.ruifu.common.vo.actanddoc.ProcessParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 流程运转过程中的相关请求方法Controller
 *
 * @author lcq
 * @create 2019/11/20
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/act")
@Api(tags = "流程管理主业务Controller")
public class ActController {

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    private ProcessActService processActService;

    @Autowired
    private ImageService imageService;

    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    /**
     * 提交(启动流程实例)
     *
     * @param processParams
     * @return
     */
    @PostMapping(value = "/submitDocument")
    @ApiOperation(value = "提交(启动流程实例)", notes = "传入参数为构造的参数，具体值可以点击查看")
    public Flag submitDocument(@RequestBody ProcessParams processParams) {
        Flag flag = processActService.submit(processParams, cloudUser.getTenantId());
        return flag;
    }

    /**
     * 查看当前用户待审核
     */
    @PostMapping(value = "/iReview/{page}/{size}")
    @ApiOperation(value = "查看当前用户待审核", notes = "根据当前登录用户信息，查询对应本id和对应角色的待审核信息")
    public PageR<List<ActivitiVO>> iReview(@PathVariable(value = "page", required = false) Integer page,
                                           @PathVariable(value = "size", required = false) Integer size) {

        return processActService.iReview(cloudUser.getId(), page, size, cloudUser.getTenantId());
    }

    /**
     * 查看当前用户已审核
     */
    @PostMapping(value = "/queryDoneTasks/{page}/{size}")
    @ApiOperation(value = "查看当前用户已审核", notes = "根据当前登录用户信息，查询对应本id的已经审核信息")
    public PageR<List<ActivitiVO>> queryDoneTasks(@PathVariable(value = "page", required = false) Integer page,
                                                  @PathVariable(value = "size", required = false) Integer size) {
        return processActService.queryDoneTasks(cloudUser.getId(), page, size, cloudUser.getTenantId());
    }

    /**
     * 审核
     *
     * @param processParams
     * @return
     */
    @PostMapping(value = "/pass")
    @ApiOperation(value = "审批节点", notes = "传入参数为构造的参数，具体值可以点击查看")
    public Flag flag(@RequestBody ProcessParams processParams) {
        return processActService.pass(processParams);
    }

    /**
     * 获取下一节点信息
     *
     * @param taskId
     * @param flag
     * @return
     */
    @GetMapping(value = "/nextTask/{taskId}/{Flag}")
    @ApiOperation(value = "获取下一节点信息", notes = "返回下一节点的名称信息")
    public List<String> nextTaskName(@PathVariable("taskId") String taskId, @PathVariable("Flag") String flag) {
        return processActService.nextTaskName(taskId, flag);
    }

    /**
     * 获取当前正在执行节点的节点信息
     *
     * @param businessKey 业务对象传过来的唯一流程标识
     * @return
     */
    @GetMapping(value = "/nowTask/businessKey/{Id}")
    @ApiOperation(value = "获取当前正在执行节点的节点信息", notes = "根据业务id获取对应当前正在执行的节点信息")
    public ProccessTaskVO getNowTaskNode(@PathVariable("Id") String businessKey) {
        return processActService.getNowTaskNode(businessKey);
    }

    /**
     * 根据流程定义id查看流程图
     *
     * @param processDefinitionId
     * @return
     */
    @GetMapping(value = "/image/{processDefinitionId}")
    @ApiOperation(value = "根据流程定义id查看流程图", notes = "根据流程定义id，查询返回不带节点流转信息的图片")
    public void image(@PathVariable("processDefinitionId") String processDefinitionId, HttpServletResponse response) {

        try (OutputStream out = response.getOutputStream();
             InputStream is = imageService.getFlowImgByProcInstId(processDefinitionId, null, false)) {
            if (null != is) {
                BufferedImage image = ImageIO.read(is);

                response.setContentType("image/png");
                ImageIO.write(image, "png", out);
            }
        } catch (Exception ex) {
            log.error("查看流程图失败", ex);
        }
    }

    /**
     * 查看流程审批全记录
     *
     * @param processInstanceId
     * @return
     */
    @GetMapping(value = "/AuditRecord/{processInstanceId}")
    @ApiOperation(value = "查看流程审批全记录", notes = "根据流程实例id，查询当前执行实例的所有历史和当前执行节点信息")
    public List<AuditRecordVO> auditRecordList(@PathVariable("processInstanceId") String processInstanceId) {
        return processActService.AuditRecordList(processInstanceId);
    }

}
